package com.lbh.cloudgallery.service.impl;

import com.lbh.cloudgallery.entity.CartEntity;
import com.lbh.cloudgallery.entity.OrderItemEntity;
import com.lbh.cloudgallery.entity.vo.OrderItemVos;
import com.lbh.cloudgallery.service.OrderItemService;

import com.lbh.cloudgallery.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.OrderDao;
import com.lbh.cloudgallery.entity.OrderEntity;
import com.lbh.cloudgallery.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ProductService productService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).eq("member_id",key)
                    .or().like("order_sn",key)
                    .or().like("member_username",key)
                    .or().like("receiver_name",key);
        }
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Long createOrder(OrderItemVos vo) {

        /**
         * 创建一个订单
         */
        OrderEntity orderEntity = new OrderEntity();
        /**
         * 生成一个单号
         */
        String orderSn = UUID.randomUUID().toString().substring(0, 5);

        /**
         * 将数据存入order表中
         */
        orderEntity.setMemberId(vo.getMemberId());
        orderEntity.setOrderSn(orderSn);
        orderEntity.setGmtCreate(new Date());
        orderEntity.setMemberUsername(vo.getMemberUsername());
        orderEntity.setStatus(0);
        orderEntity.setReceiverDetailAddr(vo.getReceiverDetailAddr());
        orderEntity.setReceiverName(vo.getReceiverName());
        orderEntity.setReceiverPhone(vo.getReceiverPhone());

        //保存数据
        boolean save = this.save(orderEntity);

        /**
         * 将OrderItemVos.OrderItems中的数据放入List中
         */
        List<OrderItemVos.OrderItems> orderItemsList = new ArrayList<>();
        orderItemsList=vo.getOrderItems();

        OrderEntity entity = this.getOne(new QueryWrapper<OrderEntity>().eq("order_sn", orderSn));

        /**
         * 遍历，逐个存入orderItem表中
         */
        for (int i = 0; i < orderItemsList.size(); i++) {

            OrderItemVos.OrderItems items = orderItemsList.get(i);
            OrderItemEntity orderItemEntity = new OrderItemEntity();
            orderItemEntity.setOrderId(entity.getId());
            orderItemEntity.setProductId(items.getProductId());
            orderItemEntity.setProductImage(items.getProductImage());
            orderItemEntity.setOrderSn(orderSn);
            orderItemEntity.setProductName(items.getProductName());
            orderItemEntity.setCount(items.getCount());
            orderItemEntity.setPrice(items.getPrice());
            orderItemService.save(orderItemEntity);
        }
        /**
         * 返回订单的id
         */
        return entity.getId();
    }

    @Override
    public boolean changeStatus(Long orderId, Integer status) {

        OrderEntity orderEntity = this.getById(orderId);
        orderEntity.setStatus(status);
        this.updateById(orderEntity);

        return true;
    }

}